package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.battle.CrossBattleResult;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.BattlePb;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_TRADE_WAR_TO_ENEMY_BATTLE, desc = "跨服商战到对手区服战斗")
public class CrossTradeWarToEnemyBattleCmd extends AbstractCommand {
	private int activityId;
	private int battleType;
	private long myUserId;
	private long myServerId;
	private List<UserPatrons> patronsList;
	private long enemyUserId;
	private int useItemId;
	private int buyBuffTimes;
	private int addScoreSkillAddition;
	private int loseScoreSkillAddition;
	private UserBaseInfo myBaseInfo;
	private long myScore;
	private String battleUid;
	private boolean isWantedOverTime;
	private int rewardSkillAddition;
	private int wantedItemId;
	private CrossBattleResult battleResult;
	private int fastTeamNum;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarToEnemyBattleReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarToEnemyBattleReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		battleType = reqMsg.getBattleType();
		myUserId = reqMsg.getMyUserId();
		myServerId = reqMsg.getMyServerId();
		patronsList = new ArrayList<>();
		for(PatronsProto.CrossPatronsTempMsg patronsTempMsg : reqMsg.getPatronsListList()){
			UserPatrons userPatrons = PatronsPb.parseUserPatrons(myUserId, patronsTempMsg);
			patronsList.add(userPatrons);
		}
		enemyUserId = reqMsg.getEnemyUserId();
		useItemId = reqMsg.getUseItemId();
		buyBuffTimes = reqMsg.getBuyBuffTimes();
		addScoreSkillAddition = reqMsg.getAddScoreSkillAddition();
		loseScoreSkillAddition = reqMsg.getLoseScoreSkillAddition();
		myScore = reqMsg.getMyScore();
		myBaseInfo = PlayerBasePb.parseBaseUserInfo(reqMsg.getMyBaseInfo());
		isWantedOverTime = reqMsg.getIsWantedOverTime();
		battleUid = reqMsg.getBattleUid();
		rewardSkillAddition = reqMsg.getRewardSkillAddition();
		wantedItemId = reqMsg.getWantedItemId();
		fastTeamNum = reqMsg.getFastTeamNum();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
            ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_NO_FOUND_BATTLE_FAIL;
			return;
		}
		battleResult = TradeWarActivityMgr.toEnemyBattle(activityId, battleType, enemyUserId, myUserId, patronsList, useItemId, buyBuffTimes,
				addScoreSkillAddition, loseScoreSkillAddition, myBaseInfo, myScore, isWantedOverTime, rewardSkillAddition, wantedItemId, fastTeamNum);
	}

	@Override
	public void sendData(GamePlayer player) {
		CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg.newBuilder();
		respMsg.setActivityId(activityId);
		respMsg.setMyUserId(myUserId);
		respMsg.setMyServerId(myServerId);
		respMsg.setRet(ret);
		respMsg.setFastTeamNum(fastTeamNum);
		if(0 == ret && null != battleResult){
			respMsg.setBattleUid(battleUid);
			respMsg.setUseItemId(useItemId);
			respMsg.setBattleType(battleResult.getBattleType());
			respMsg.setRecordUid(battleResult.getRecordUid());
			respMsg.setWantedItemId(wantedItemId);
			respMsg.setTrackChangeType(battleResult.getTrackChangeType());
			respMsg.setBattleDetailList(BattlePb.parseBattleDetailListMsg(battleResult.getPatronsBattleDetailList()));
			//对手出战门客
			for (UserPatrons userPatrons : battleResult.getEnemyPatronsList()) {
				PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
				if(null != patronsMsg) {
					respMsg.addEnemyPatronsList(patronsMsg);
				}
			}
			respMsg.addAllPatronsIdList(battleResult.getMyPatronsIdList());
			respMsg.setKillPatronsCount(battleResult.getKillPatronsCount());
			respMsg.setMyAddScore(battleResult.getMyAddScore());
			respMsg.setEnemyLoseScore(battleResult.getEnemyLoseScore());
			respMsg.setEnemyUserId(enemyUserId);
			respMsg.setMyBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(myBaseInfo));
			respMsg.setEnemyBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(battleResult.getEnemyBaseInfo()));
			respMsg.setBattleReward(battleResult.getReward());
			respMsg.setFastTeamNum(battleResult.getTeamIndex());
		}
		int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, myUserId);
		GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_TO_ENEMY_BATTLE_RESP, respMsg, 0, activityId, groupId);
	}

}
